package com.jp.knowledge.my.activity;

import android.content.Intent;

import com.google.gson.JsonObject;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.my.resources.OrganizeJoinRes;
import com.jp.knowledge.util.ResultSubscriber;

public class DepartmentCreateActivity extends PositionActivity implements ResultSubscriber.OnResultListener {

    protected Intent intent;
    protected int requestCode;

    @Override
    protected int getResourceId() {
        return R.layout.activity_position;
    }

    @Override
    protected void init() {
        super.init();
        intent = getIntent();
        requestCode = intent.getIntExtra(OrganizeJoinRes.REQUEST_CODE, 0);
        initTitle(intent.getStringExtra(OrganizeJoinRes.TITLE_CREATE));
        editText.setHint(intent.getStringExtra(OrganizeJoinRes.REMIND_CREATE));
    }

    @Override
    protected void editFinishBnClick() {
        if(editText.getText().toString().trim().length() == 0){
            ToasUtil.toast(this, R.string.toast_department_create_params_remind);
        } else{
            loading.show();
            JsonObject param = new JsonObject();
            param.addProperty("token", HttpHelper.token);
            param.addProperty("id", intent.getStringExtra(OrganizeJoinRes.ID));
            param.addProperty("name", editText.getText().toString());
            if(requestCode == OrganizeJoinRes.SCHOOL_INDEX) {
                param.addProperty("depaId", intent.getStringExtra(OrganizeJoinRes.DEPA_ID));
                HttpHelper.getInstance(this).createClass(param, requestCode, this);
                return;
            }
            param.addProperty("type", requestCode + 1);
            HttpHelper.getInstance(this).createDepartment(param, requestCode, this);
        }
    }

    @Override
    public void onStart(int requestType) {}
    @Override
    public void onCompleted(int requestType) {
        loading.cancel();
    }
    @Override
    public void onError(int requestType) {
        loading.cancel();
    }
    @Override
    public void onNext(IModel data, int requestType) {
        if(data.getErrcode() != 0){
            ToasUtil.toast(mContext, data.getMessage());
        } else if(requestType == this.requestCode) {
            setResult(RESULT_OK, intent);
            finish();
        }
    }
}
